<template>
  <!--  isShowAddBtn添加按钮  sShowEdit删除按钮 isShowStatus开启状态  isShowLinkUrl链接地址 isShowImageUrl图片地址-->
  <!--  tips提示语，list列表数据， title文本输入框标题-->
  <div class="hot_imgs">
    <div class="list-box mt20" v-if="configData.length">
      <draggable class="dragArea list-group" :list="configData" group="peoples" handle=".move-icon">
        <div class="item" v-for="(item, index) in configData" :key="index">
          <div class="move-icon">
            <span class="iconfont icon-tuozhuaidian"></span>
          </div>
          <div v-if="['service_recommond','service_tabbar'].indexOf(name) === -1" class="img-box" @click="modalPicTap(item, index,0)">
            <img :src="item.pic" alt="" v-if="item.pic" />
            <div class="upload-box" v-else><i class="el-icon-camera-solid" style="font-size: 30px" ></i></div>
          </div>
          <div v-if="['service_tabbar'].indexOf(name) !== -1" class="img-box" @click="modalPicTap(item, index,0)">
            <img :src="item.pic[0]" alt="" v-if="item.pic" />
            <div class="upload-box" v-else><i class="el-icon-camera-solid" style="font-size: 30px" ></i></div>
          </div>
          <div v-if="['service_tabbar'].indexOf(name) !== -1" class="img-box" @click="modalPicTap(item, index,1)">
            <img :src="item.pic[1]" alt="" v-if="item.pic" />
            <div class="upload-box" v-else><i class="el-icon-camera-solid" style="font-size: 30px" ></i></div>
          </div>
          
          <div class="info">
            <div class="info-item">
              <span class="text-14px">标题</span>
              <div class="input-box">
                <el-input
                  size="small"
                  v-model="item.name"
                  placeholder="请输入标题"
                ></el-input>
              </div>
            </div>
            <div v-if="['service_recommond','service_bottom_icon'].indexOf(name) !== -1" class="info-item">
              <span class="text-14px">副标题</span>
              <div class="input-box">
                <el-input
                  size="small"
                  v-model="item.subname"
                  placeholder="请输入副标题"
                ></el-input>
              </div>
            </div>
            <div v-if="['service_recommond'].indexOf(name) !== -1" class="info-item">
              <span class="text-14px">分类：</span>
              <div class="input-box">
                <el-cascader v-model="item.cate_value" @change="cateChange" :options="cateLists" clearable></el-cascader>
              </div>
            </div>
            <div v-if="['service_recommond','service_bottom_icon'].indexOf(name) === -1" class="info-item">
              <span class="text-14px">链接地址</span>
              <div class="input-box" @click="getLink(index, item.url)">
                <el-input size="small" v-model="item.url" :placeholder="'请选择链接'">
                  <el-button slot="append" icon="el-icon-arrow-right"></el-button>
                </el-input>
              </div>
              <!-- <div v-else class="input-box">
                <el-input size="small" v-model="item.url" placeholder="请输入链接"></el-input>
              </div> -->
            </div>
            <div v-if="['service_recommond'].indexOf(name) !== -1" class="info-item">
              <span class="text-14px">是否热门</span>
              <div class="input-box">
                <el-switch
                style="width:55px;"
                  v-model="item.is_hot"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#2d8cf0">
                </el-switch>
              </div>
            </div>
            <div v-if="isEditBtn" class="delect-btn" @click.stop="handleDelete(item, index)">
              <i class="el-icon-error" style="font-size: 26px" />
            </div>
          </div>
        </div>
      </draggable>
    </div>
    <template v-if="isShowAddBtn">
      <div class="add-btn mt20" v-if="configData.length < modelMaxLength || !modelMaxLength">
        <el-button class="button" icon="el-icon-plus" plain @click="handleAddBox">添加板块</el-button>
      </div>
    </template>
    <linkaddress ref="linkaddres" @linkUrl="linkUrl"></linkaddress>
  </div>
</template>

<script>
// +---------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +---------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +---------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +---------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +---------------------------------------------------------------------
import vuedraggable from 'vuedraggable';
import linkaddress from '@/components/linkaddress';
export default {
  name: 'c_menu_list',
  props: {
    configObj: {
      type: Array,
      default:[],
    },
    modelMaxLength: {
      type: Number,
      default:0,
    },
    name: {
      type: String,
      default:' ',
    },
    isRub: {
      type: Boolean,
      default: false,
    },
    isEditBtn: {
      type: Boolean,
      default: true,
    },
    isShowAddBtn: {
      type: Boolean,
      default: true,
    },

    cateList:{
      type: Array,
      default:[],
    },
  },
  computed:{
    cateLists(){
      return this.cateList||[];
    },
  },
  components: {
    draggable: vuedraggable,
    linkaddress,
  },
  data() {
    return {
      defaults: {},
      configData: [],
      activeIndex: 0, //索引
      lastObj: {},
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.configData = this.configObj;
    });
  },
  watch: {
    configObj: {
      handler(nVal, oVal) {
        this.configData = nVal;
      },
      deep: true,
    },
  },
  methods: {
    cateChange(e){
      console.log(e,'choose_cate')
    },
    linkUrl(e) {
      this.configData[this.activeIndex].url = e;
    },
    getLink(index, item) {
      this.activeIndex = index;
      this.$refs.linkaddres.dialogVisible = true;
    },
    //删除链接
    handleLinkDelete(item, indexs, index) {
      this.configData[index].splice(indexs, 1);
    },
    //添加板块
    handleAddBox() {
      let data = {
        name: '',
        url: '',
        pic:'',
        // subname:'',
        // is_hot:0,
        // cate_value:[],
      };
      if(this.name == 'service_recommend'){
        data.subname = '';
        data.is_hot = 0;
        cate_value = [];
      }
      else if(this.name == 'service_tabbar'){
        data.pic = [0,0];
      }
      this.configData.push(data);
      console.log(this.configData,'configData',this.name);
    },
    // 点击图文封面
    modalPicTap(item, index,att) {
      this.activeIndex = index;
      let _this = this;
      _this.$modalUpload(function (img) {
        if (!img) return;
        item.img = img[0].sattDir;
        _this.getPic(img[0].sattDir,att);
      }, false,'diy');
    },
    // 获取图片信息
    getPic(pc,index) {
      this.$nextTick(() => {
        if(this.name == 'service_tabbar'){
          this.configData[this.activeIndex].pic.splice(index,1,pc);
        }else{
          this.configData[this.activeIndex].pic = pc;
        }
      });
    },
    
    // 删除整个模块
    handleDelete(item, index) {
      if (this.configData.length == 1) {
        this.lastObj = this.configData[0];
      }
      this.configData.splice(index, 1);
    },
  },
};
</script>

<style scoped lang="scss">
.delect-btn-link {
  position: absolute;
  right: -9px;
  top: -9px;
  cursor: pointer;
}

.hot_imgs {
  width: 100%;
  margin-bottom: 20px;

  .list-box {
    border: 1px dashed #cccccc;
    padding: 20px;

    .item {
      width: 100%;
      background: #f9f9f9;
      border-radius: 3px 3px 3px 3px;
      opacity: 1;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 20px;
      margin-bottom: 14px;

      .move-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 64px;
        cursor: move;
      }

      .img-box {
        position: relative;
        width: 64px;
        height: 64px;
        margin-left: 20px;
        border-radius: 3px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 3px;
        }
      }
    }

    .info {
      flex: 1;
      margin-left: 22px;

      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;

        span {
          width: 60px;
          font-size: 12px;
          color: #999999;
        }

        .input-box {
          flex: 1;

          ::v-deep.ivu-input {
            font-size: 13px !important;
          }
        }
      }
    }

    .delect-btn {
      position: absolute;
      right: -7px;
      top: -12px;

      .iconfont-diy,
      .iconfont {
        font-size: 25px;
        color: #999;
      }
    }
  }

  .add-btn .button {
    width: 100%;
    height: 36px;
  }
}

.upload-box {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: #fff;
  font-size: 12px;
  color: #cccccc;

  .iconfont {
    font-size: 16px;
  }
}

.iconfont-diy,
.iconfont {
  color: #dddddd;
  font-size: 28px;
}

.el-select {
  display: block;
}
</style>
